/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wakeonlan;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import plugin.PluginInterface;
import system.MessageOUTPUT;

/**
 *
 * @author ZeM
 */
public class Main implements PluginInterface {

    static boolean stop;
    static boolean crashed;
    static String crasherror;

    String[][] Plugin_String_parameter;
    String[][][][] Plugin_Group_String_parameter;

    public boolean hasParameters() {
        return true;
    }

    public String[] ParameterNames() {
        return null;
    }

    public String[] GroupNames() {
        String[] p = {"Group"};
        return p;
    }

    public String[][] GroupParameterNames() {
        String[][] p = new String[1][2];
        p[0][0] = "Mask";
        p[0][1] = "Mac";
        return p;
    }

    public String About() {
        return new MessageOUTPUT().PluginMessage("WakeOnLan","about", null);
    }

    public boolean Crashed() {
        return crashed;
    }

    public String CrashError() {
        return crasherror;
    }

    public void Start() {
        stop = false;
        crashed = false;
        Run();
    }

    public void Stop() {
        stop = true;
    }

    private void Run() {
        int PORT = 9;

        for (int j = 0; j < Plugin_Group_String_parameter[0].length; j++) {


            String ipStr = Plugin_Group_String_parameter[0][j][0][0];

            for (int k = 0; k < Plugin_Group_String_parameter[0][j][1].length; k++) {
                String macStr = Plugin_Group_String_parameter[0][j][1][k];

                try {
                    byte[] macBytes = getMacBytes(macStr);
                    byte[] bytes = new byte[6 + 16 * macBytes.length];
                    for (int i = 0; i < 6; i++) {
                        bytes[i] = (byte) 0xff;
                    }
                    for (int i = 6; i < bytes.length; i += macBytes.length) {
                        System.arraycopy(macBytes, 0, bytes, i, macBytes.length);
                    }

                    InetAddress address = InetAddress.getByName(ipStr);
                    DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, PORT);
                    DatagramSocket socket = new DatagramSocket();
                    socket.send(packet);
                    socket.close();
                    String[] message = {macStr, ipStr};
                    new MessageOUTPUT().PluginOutput("WakeOnLan","m0001", message); //Wake-on-LAN packet sent.
                } catch (Exception e) {
                    String[] message = {macStr, ipStr};
                    crashed = true;
                    crasherror = new MessageOUTPUT().PluginMessage("WakeOnLan","er0001", message); //Failed to send Wake-on-LAN packet


                }
            }
        }
    }

    private static byte[] getMacBytes(String macStr) throws IllegalArgumentException {
        byte[] bytes = new byte[6];
        String[] hex = macStr.split("(\\:|\\-)");
        if (hex.length != 6) {
            String[] message = {macStr};
            crashed = true;
            crasherror = new MessageOUTPUT().PluginMessage("WakeOnLan","er0002", message); //Invalid MAC address.

        }
        try {
            for (int i = 0; i < 6; i++) {
                bytes[i] = (byte) Integer.parseInt(hex[i], 16);
            }
        } catch (NumberFormatException e) {
            String[] message = {macStr};
            crashed = true;
            crasherror = new MessageOUTPUT().PluginMessage("WakeOnLan","er0003", message);//Invalid hex digit in MAC address

        }
        return bytes;
    }

    public void InputParameters(String[][] parameter, String[][][][] group_parameter) {
        Plugin_String_parameter=parameter;
        Plugin_Group_String_parameter=group_parameter;
    }
}

